package com.jackeysun.widget.adapter

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

/**
 *   Created by sunxunchao on 2022/3/18.
 */
open class SimpleAdapter<T>(val dataSource: DataSource<T>) :
    RecyclerView.Adapter<SimpleViewHolder>() {

    init {
        dataSource.adapter = this
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimpleViewHolder =
        dataSource.onCreateViewHolder(parent, viewType)

    override fun onBindViewHolder(
        holder: SimpleViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        super.onBindViewHolder(holder, position, payloads)
        dataSource.onBindViewHolder(holder, position, payloads)
    }

    override fun onBindViewHolder(holder: SimpleViewHolder, position: Int) {
        dataSource.onBindViewHolder(holder, position)
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        dataSource.onAttachedToRecyclerView(recyclerView)
    }

    override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
        super.onDetachedFromRecyclerView(recyclerView)
        dataSource.onDetachedFromRecyclerView(recyclerView)
    }

    override fun onViewDetachedFromWindow(holder: SimpleViewHolder) {
        super.onViewDetachedFromWindow(holder)
        dataSource.onViewDetachedFromWindow(holder)
    }

    override fun onViewAttachedToWindow(holder: SimpleViewHolder) {
        super.onViewAttachedToWindow(holder)
        dataSource.onViewAttachedToWindow(holder)
    }

    override fun onViewRecycled(holder: SimpleViewHolder) {
        super.onViewRecycled(holder)
        dataSource.onViewRecycled(holder)
    }

    override fun getItemCount(): Int = dataSource.getItemCount()

    override fun getItemViewType(position: Int): Int = dataSource.viewType(position)
}